package logic

import (
	"context"
	"errors"
	"fmt"
	"user_srv/model/mysql"
	"user_srv/pkg"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *user.UserLoginRequest) (*user.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{}
	err := u.UserInfoUsername(u.UserName)
	if err != nil {
		logx.Error("登录失败", err)
		return nil, errors.New("登录失败1")
	}
	if u.ID == 0 {
		fmt.Println("账号未注册当前自动注册")
		u.UserName = in.Username
		u.Password = pkg.Md5Password(in.Password)
		u.Phone = in.Phone
	}
	return &user.UserLoginResponse{
		Userid: int64(u.ID),
	}, nil
}
